<?php

namespace App\Modules\Device\Services;
use Illuminate\Support\Facades\DB;
use  App\Modules\Device\Models\Device;



class DeviceService
{

    //设备列表
    public static function getList($params){
        $where['a.status'] =1;
        if(isset($params['device_code']) && $params['device_code']) {
            $where['a.device_code'] = $params['device_code'];

        }

        if(isset($params['device_type']) && $params['device_type']) {
            $where['t.device_type'] =  $params['device_type'];
        }

        if( isset($params['is_bundled']) && $params['is_bundled'] == 'NO_BUND' ) $where['a.is_bundled'] = '0';

        if(isset($params['is_bundled']) && $params['is_bundled'] == '1') $where['a.is_bundled'] = $params['is_bundled'];

        $res = DB::table('device as a')
            ->select('a.*','t.brand_name','t.facory_name','t.device_type')
            ->leftJoin('facory as t','a.facory_id','=','t.id')
            ->where($where)
            ->orderBy('a.id','desc')
            ->paginate(10);
        return $res;
    }

    //添加设备
    public static function addDevice($param){
        return Device::addDevice($param);

    }

    public static function oneDevice($id){
        $res = DB::table('device as a')
            ->select('a.*','t.brand_name','t.id as fid')
            ->leftJoin('facory as t','a.facory_id','=','t.id')
            ->where('a.id',$id)
            ->orderBy('a.id','desc')
            ->get()->toArray();
//        print_r($res);
        return $res;
    }

    //更新设别
    public static function upDevice($id,$param){

        return Device::where('id',$id)->update($param);
    }

}